package com.ch09.service.impl;

import com.ch09.entity.City;
import com.ch09.mapper.CityMapper;
import com.ch09.service.CityService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CityServiceImpl
 * @Description TODO
 * @Author Kx
 * @Date 2025/5/18 19:51
 * @Version 1.0
 */
@Service("cityService")
// 事务注解(当前类中的所有方法都会参与到事务中)
// spring的事务是基于AOP实现的
// rollbackFor: 用于设置当遇到什么异常就回滚
// propagation: 用于设置事务的传播级别
@Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
public class CityServiceImpl implements CityService {

    private CityMapper cityMapper;

    public CityServiceImpl(CityMapper cityMapper) {
        this.cityMapper = cityMapper;
    }

    /**
     * @param page : 当前页数
     * @param size : 每页显示的条数
     * @return com.github.pagehelper.PageInfo<com.ch09.entity.City> : 分页信息
     * @author: kx
     * @date: 2025/5/18 19:53
     * @description: 分页查询所有城市信息
     */
    // 只读事务,当设置readOnly=true时,其他事务将不能参与当前事务
    @Transactional(readOnly = true)
    @Override
    public PageInfo<City> findAll(int page, int size) {
        // 分页查询
        List<City> all = cityMapper.findAll(page, size);
        return new PageInfo<>(all);
    }
}
